<?php
class categoryEditAction extends sfActions
{
	public function execute($request)
	{
		$this->category_id = $request->getParameter('category_id');
		if ($this->category_id) {
			$this->cms_category = Doctrine_Core::getTable('DtCmsCategory')->find($this->category_id);
			if ($this->category_id)
				$this->cms_category_form = new AdminCmsCategoryForm($this->cms_category);
		}
		
		if ($request->hasParameter('cms_category')) {
			$form = $request->getParameter('cms_category');
			$this->status = $this->updateCmsCategoryFromRequest($this->cms_category, $form);
			if ($this->status) {
				$this->cms_category->save();
				$this->forward('cms', 'index');
			} else
				return SfView::SUCCESS;
		} else
			return SfView::SUCCESS;
		
	}
	
	
	private function updateCmsCategoryFromRequest(&$cms_category, $form)
	{
		if ($form['name'])
			$cms_category->name = $form['name'];
		if ($form['parent_id'])
			$cms_category->parent_id = $form['parent_id'];
		if ($form['position'])
			$cms_category->position = $form['position'];

		$cms_category->updated_at = date(DATE_ISO8601);
		return true;
	}
}
?>